package com.dds.system.service.admin.impl;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
import java.util.Map;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.dds.system.mapper.admin.AdminOperationShopProfitMapper;
import com.dds.system.domain.admin.AdminOperationShopProfit;
import com.dds.system.service.admin.IAdminOperationShopProfitService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 人员-店铺-比例记录Service业务层处理
 * 
 * @author gaolianjie
 * @date 2021-04-27
 */
@Slf4j
@Service
public class AdminOperationShopProfitServiceImpl implements IAdminOperationShopProfitService 
{
    @Autowired
    private AdminOperationShopProfitMapper adminOperationShopProfitMapper;

    @Autowired
    private AdminUserOperationChildServiceImpl adminUserOperationChildService;

    /**
     * 查询人员-店铺-比例记录
     * 
     * @param id 人员-店铺-比例记录ID
     * @return 人员-店铺-比例记录
     */
    @Override
    public AdminOperationShopProfit selectAdminOperationShopProfitById(Long id)
    {
        return adminOperationShopProfitMapper.selectAdminOperationShopProfitById(id);
    }

    /**
     * 查询人员-店铺-比例记录列表
     * 
     * @param adminOperationShopProfit 人员-店铺-比例记录
     * @return 人员-店铺-比例记录
     */
    @Override
    public List<AdminOperationShopProfit> selectAdminOperationShopProfitList(AdminOperationShopProfit adminOperationShopProfit)
    {
        return adminOperationShopProfitMapper.selectAdminOperationShopProfitList(adminOperationShopProfit);
    }

    /**
     * 新增人员-店铺-比例记录
     * 
     * @param adminOperationShopProfit 人员-店铺-比例记录
     * @return 结果
     */
    @Override
    public int insertAdminOperationShopProfit(AdminOperationShopProfit adminOperationShopProfit)
    {
        return adminOperationShopProfitMapper.insertAdminOperationShopProfit(adminOperationShopProfit);
    }

    /**
     * 修改人员-店铺-比例记录
     * 
     * @param adminOperationShopProfit 人员-店铺-比例记录
     * @return 结果
     */
    @Override
    public int updateAdminOperationShopProfit(AdminOperationShopProfit adminOperationShopProfit)
    {
        return adminOperationShopProfitMapper.updateAdminOperationShopProfit(adminOperationShopProfit);
    }

    /**
     * 批量删除人员-店铺-比例记录
     * 
     * @param ids 需要删除的人员-店铺-比例记录ID
     * @return 结果
     */
    @Override
    public int deleteAdminOperationShopProfitByIds(Long[] ids)
    {
        return adminOperationShopProfitMapper.deleteAdminOperationShopProfitByIds(ids);
    }

    /**
     * 删除人员-店铺-比例记录信息
     * 
     * @param id 人员-店铺-比例记录ID
     * @return 结果
     */
    @Override
    public int deleteAdminOperationShopProfitById(Long id)
    {
        return adminOperationShopProfitMapper.deleteAdminOperationShopProfitById(id);
    }

    /**
     * 创建新数据
     * @param serveUser 服务商
     * @param shopId 店铺id
     * @return 结果
     */
    @Transactional
    public boolean createProfit(String serveUser, String shopId) {
        // 获取开店人员的运营团队比例
        List<Map<String, Object>> operationProportion = adminUserOperationChildService.getOperationProportion(serveUser);

        for (Map<String, Object> info : operationProportion) {
            // 删除旧数据
            adminOperationShopProfitMapper.selectProfitAndDeleteByUserIdShopId(info.get("userId").toString(), shopId);

            AdminOperationShopProfit adminOperationShopProfit = AdminOperationShopProfit.of()
                    .setUserId(info.get("userId").toString())
                    .setShopId(shopId)
                    .setProportion(new BigDecimal(info.get("proportion").toString()))
                    .setLevel(Integer.parseInt(info.get("level").toString()))
                    .setIsOwner(info.get("isOwner").toString())
                    .setCreatedAt(new Date())
                    .setUpdatedAt(new Date());

            adminOperationShopProfitMapper.insertAdminOperationShopProfit(adminOperationShopProfit);
            log.info("人店比新数据-----" + adminOperationShopProfit);
        }

        return true;
    }
}
